#include <rtthread.h>
#include <stm32f4xx.h>
#include <string.h>
#include <dfs_posix.h>
#include "utils.h"
#include "board.h"

char *string_cyclicshift_v2( char *string, int i )
{
    int exchange;
    int len;
    int start_pos = 0;

    exchange = 0;
    len = strlen( string );

    i = i % len;
    if ( 0 == i )
        return string;

    while ( exchange < len )
    {
        char ch = string[start_pos];

        int currpos = start_pos;
        int nextpos = (len + currpos + i) % len;
        while ( nextpos != start_pos )
        {
            string[currpos] = string[nextpos];
            ++exchange;

            currpos = nextpos;
            nextpos = (len + currpos + i) % len;
        }
        string[currpos] = ch;
        ++exchange;

        ++start_pos;
    }

    return string;
}

rt_uint32_t read_line(int fd, char *line, rt_uint32_t line_size)
{
    char *pos, *next;
    rt_uint32_t length;

    length = read(fd, line, line_size);
    if (length > 0)
    {
        pos = strstr(line, "\r\n");
        if (pos == RT_NULL)
        {
            pos = strstr(line, "\n");
            next = pos ++;
        }
        else next = pos + 2;

        if (pos != RT_NULL)
        {
            *pos = '\0';

            /* move back */
            lseek(fd, -(length - (next - line)), SEEK_CUR);

            length = pos - line;
        }
        else length = 0;
    }

    return length;
}

rt_bool_t is_exist(const char *path)
{
    int fd;

    fd = open(path, O_RDONLY, 0);
    if (fd < 0)
    {
        close(fd);
        return RT_FALSE;
    }

    close(fd);
    return RT_TRUE;
}


rt_bool_t is_directory(const char *path)
{
    struct stat s;

    stat(path, &s);
    if ( s.st_mode & DFS_S_IFDIR )
        return RT_TRUE;

    return RT_FALSE;
}

rt_uint32_t get_filesize(int fd)
{
    struct stat s;

    fstat(fd, &s);
    return	s.st_size;

}


rt_uint32_t rt_tell(int fd)
{
    return 	 lseek(fd, 0, 1);
}


int file_type(const char *path)
{
    if (rt_strstr(path, ".mp3") != RT_NULL || rt_strstr(path, ".MP3") != RT_NULL)
    {
        return FILE_MP3;
    }
    else if (rt_strstr(path, ".wav") != RT_NULL || rt_strstr(path, ".WAV") != RT_NULL)
    {
        return FILE_WAV;
    }
    else
    {
        return FILE_UN;
    }
}

void fn2name(char* fn, char* buffer)
{
	rt_uint16_t	pos=0;
	rt_uint16_t i;

	for(i=0;i<1024;i++)
	{
		if(fn[i]=='/')
		{
			pos = i+1;	
		}
		if(fn[i]<0x80&&fn[i+1]==0)
		{
			rt_memcpy(buffer,&fn[pos],(i-pos+1));
			break;		
		}
	}
}

void fn2folder(char* fn, char* buffer)
{
	rt_uint16_t	pos=0;
	rt_uint16_t i;

	for(i=0;i<1024;i++)
	{
		if(fn[i]=='/'&&i>0)
		{
			pos = i;	
		}
		if(fn[i]=='\0')
		{
			rt_memcpy(buffer,&fn[0],pos);
			break;
		}			
	}
}

rt_uint8_t get_path_depth(const char* fn)
{
	rt_uint8_t	depth=0;
	rt_uint16_t i;

	for(i=0;i<512;i++)
	{
		if(fn[i]=='\0')
		{
			break;
		}
		if(fn[i]=='/'&&i>0)
		{
			depth++;	
		}
	}

	return depth;
}
